package com.atts.android.Baccarat;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebSettings.ZoomDensity;
import android.webkit.WebView;
import android.widget.Toast;

import com.atts.baccarat.table.Dashboard;
import com.atts.baccarat.table.DisplayDashboard;
import com.atts.baccarat.table.TableItemColor;
import com.google.gson.Gson;

public class IndexActivity extends Activity {

	/**
	 * 浏览器窗口
	 */
	private WebView myWebView;

	/**
	 * 后台计算模型
	 */
	private Dashboard dashboard;

	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		this.setContentView(R.layout.index);

		this.initRes();
		this.findViews();
		this.setValues();
		this.setListeners();
	}

	private void initRes() {
		this.dashboard = new Dashboard();
	}

	private void findViews() {
		this.myWebView = (WebView) this.findViewById(R.id.webview);
	}

	private void setValues() {

		// 获取 WebSetting
		final WebSettings webSettings = this.myWebView.getSettings();
		webSettings.setDefaultZoom(ZoomDensity.FAR);

		// 开启Web View对JavaScript的支持
		webSettings.setJavaScriptEnabled(true);
		this.myWebView.setHorizontalScrollBarEnabled(false);
		this.myWebView.setVerticalScrollBarEnabled(false);
		this.myWebView.setKeepScreenOn(true);

		this.myWebView.addJavascriptInterface(new BackgroundInterface(this),
		"Background");

		this.myWebView.loadUrl("file:///android_asset/www/table.html");

	}

	private void setListeners() {

	}

	/**
	 * JS, JAVA 交互接口
	 * 
	 * @author Administrator
	 * 
	 */
	final class BackgroundInterface {

		private final Context mContext;

		BackgroundInterface(final Context c) {
			this.mContext = c;
		}

		/**
		 * reload page
		 */
		public void reload() {
			IndexActivity.this.myWebView.reload();
		}

		/**
		 * 显示提示信息
		 * 
		 * @param toast
		 *            msg
		 */
		public void showToast(final String toast) {
			Toast.makeText(this.mContext, toast, Toast.LENGTH_LONG).show();
		}

		/**
		 * 
		 * @param result
		 *            0, 庄赢，　1,闲赢, 2，和
		 * @return Dashboard JSON
		 */
		public String addResult(final int result) {

			TableItemColor color = null;
			if (result == 0) {
				color = TableItemColor.RED;
			} else if (result == 1) {
				color = TableItemColor.BLUE;
			} else if (result == 2) {
				color = TableItemColor.GREEN;
			} else {
				throw new IllegalArgumentException("invalid result " + result);
			}

			IndexActivity.this.dashboard.addResult(color);

			final DisplayDashboard r = IndexActivity.this.dashboard
			.getDisplayResult();

			final String json = new Gson().toJson(r);

			Log.i("aaaaaaaaaaaaaaaaaaaaa", json);

			return json;
		}

		/**
		 * 清空所有
		 * 
		 */
		public void reset() {
			IndexActivity.this.dashboard.reset();
		}

		/**
		 * 回退到上一步
		 * 
		 */
		public String previous() {
			IndexActivity.this.dashboard.previous();

			final DisplayDashboard r = IndexActivity.this.dashboard
			.getDisplayResult();

			final String json = new Gson().toJson(r);

			Log.i("aaaaaaaaaaaaaaaaaaaaa", json);

			return json;
		}
	}

}